//
//  LLNetworkHandler.m
//  LoveLove
//
//  Created by Elise on 2017/6/15.
//  Copyright © 2017年 Elvis. All rights reserved.
//

#import "LLNetworkHandler.h"
#import <AFNetworking.h>

static LLNetworkHandler *_handler = nil;

@implementation LLNetworkHandler


#pragma mark - public methods

- (void)connectURL:(NSString *)url parameter:(id)parameter networkType:(LLNetworkRequestType)type Success:(LLSuccessBlock)successCallback Failure:(LLFailureBlock)failureCallback{
    
    // 初始化网络配置
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    // 创建网络请求管理实例
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    
    // 设置请求超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = LL_REQUEST_TIME_OUT;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    // 设置服务器响应数据类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"application/x-javascript",@"text/plain",@"image/gif",nil];

    // 有中文的情况下要进行转码
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    // 根据请求方式进行请求
    if (type == LLNetworkRequestTypeGet) {
       [manager GET:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successCallback) {
                successCallback(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureCallback) {
                failureCallback(error);
            }
        }];
    }else if (type == LLNetworkRequestTypePost) {
        [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successCallback) {
                successCallback(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureCallback) {
                failureCallback(error);
            }
        }];
    }
}

- (void)connectNoSignURL:(NSString *)url parameter:(id)parameter networkType:(LLNetworkRequestType)type Success:(LLSuccessBlock)successCallback Failure:(LLFailureBlock)failureCallback{
    
    // 初始化网络配置
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    // 创建网络请求管理实例
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    
    // 设置请求超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = LL_REQUEST_TIME_OUT;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    // 设置服务器响应数据类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"application/x-javascript",@"text/plain",@"image/gif",nil];
    
    // 有中文的情况下要进行转码
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    // 根据请求方式进行请求
    if (type == LLNetworkRequestTypeGet) {
        [manager GET:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successCallback) {
                successCallback(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureCallback) {
                failureCallback(error);
            }
        }];
    }else if (type == LLNetworkRequestTypePost) {
        [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (successCallback) {
                successCallback(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureCallback) {
                failureCallback(error);
            }
        }];
    }
}

#pragma mark - Signleton

+ (instancetype)shareHandler{
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _handler = [super allocWithZone:zone];
    });
    return _handler;
}

- (instancetype)init{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _handler = [super init];
    });
    return _handler;
}

@end
